/**
 *
 */
package com.zhuoyue.zhtj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eit.eitsite.common.utils.HttpUtils;
import com.google.common.collect.Maps;
import com.zhuoyue.zhtj.common.ServerResponse;
import com.zhuoyue.zhtj.entity.*;
import com.zhuoyue.zhtj.service.KkcdtjService;
import com.zhuoyue.zhtj.service.KqjlService;
import net.sf.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.zhtj.service.KckqjlService;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

/**
 * 考勤记录Controller
 * @author hss
 * @version 2017-09-22
 */
@Controller
@RequestMapping(value = "${adminPath}/zhtj/kckqjl")
public class KckqjlController extends BaseController {

	@Autowired
	private KckqjlService kckqjlService;
	@Autowired
	private KqjlService kqjlService;
	@Autowired
	private KkcdtjService kkcdtjService;
	
	@ModelAttribute
	public Kckqjl get(@RequestParam(required=false) String id) {
		Kckqjl entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = kckqjlService.get(id);
		}
		if (entity == null){
			entity = new Kckqjl();
		}
		return entity;
	}
	
	@RequiresPermissions("zhtj:kckqjl:view")
	@RequestMapping(value = {"list", ""})
	public String list(Kckqjl kckqjl, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Kckqjl> page = kckqjlService.findPage(new Page<Kckqjl>(request, response), kckqjl); 
		model.addAttribute("page", page);
		return "zhuoyue/zhtj/kckqjlList";
	}

	@RequiresPermissions("zhtj:kckqjl:view")
	@RequestMapping(value = "form")
	public String form(Kckqjl kckqjl, Model model) {
		model.addAttribute("kckqjl", kckqjl);
		return "zhuoyue/zhtj/kckqjlForm";
	}

	@RequiresPermissions("zhtj:kckqjl:edit")
	@RequestMapping(value = "save")
	public String save(Kckqjl kckqjl, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, kckqjl)){
			return form(kckqjl, model);
		}
		kckqjlService.save(kckqjl);
		addMessage(redirectAttributes, "保存考勤记录成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/kckqjl/?repage";
	}
	
	@RequiresPermissions("zhtj:kckqjl:edit")
	@RequestMapping(value = "delete")
	public String delete(Kckqjl kckqjl, RedirectAttributes redirectAttributes) {
		kckqjlService.delete(kckqjl);
		addMessage(redirectAttributes, "删除考勤记录成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/kckqjl/?repage";
	}

	/**
	 * 应到节数接口
	 * @param kckqjl
	 * @param response
     * @return
     */
	@RequestMapping(value = "getCourse")
	public String getCourse(Kckqjl kckqjl, Kqjl kqjl,HttpServletResponse response){
		Map<String,Object> map=kckqjlService.getCourse(kckqjl);
		if(map!=null){
			kqjl.setNum(kckqjl.getNumber());
			if(kqjlService.getCourseBykqjl(kqjl)){
				List<Kkcdtj> list1=kkcdtjService.findKKList(kqjl);
				if(list1!=null){
					map.put("kuangkelist",list1);
					map.put("kuangkenum",list1.size());
				}
				List<Kkcdtj> list2=kkcdtjService.findCDList(kqjl);
				if(list2!=null){
					map.put("chidaolist",list2);
					map.put("chidaonum",list2.size());
				}
				map.put("actualnum",(int)map.get("all")-list1.size()-list2.size());
			}
			return this.renderString(response, ServerResponse.createBySuccess("获取课程节数成功",map));
		}
		return renderString(response,ServerResponse.createByErrorMessage("获取课程节数失败"));
	}

	@RequestMapping(value = "saveRecord")
	public String saveRecord(String str,HttpServletResponse response){
		if(kckqjlService.saveRecord(str)){
			return this.renderString(response,ServerResponse.createBySuccessMessage("新增记录成功"));
		}
		return this.renderString(response,ServerResponse.createByErrorMessage("新增记录失败"));
	}
}